<?php
class fileupModel
{
    public $allowtype = array("jpg","gif","jpeg","png");
    function __construct(){
        //@ini_set('memory_limit','100M');
    }
    public static function pageback($_field,$_str,$_url)
    {
        $_field=trim($_field);
        if($_field=="")
        {
            echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            if($_url=="")
            {
                die("<script>alert('".$_str."');history.go(-1);</script>") ;
            }
            else
            {
                die("<script>alert('".$_str."');document.location.href='".$_url."';</script>") ;
            }
        }
    }

    public static function madedir($filedir="")
    {
        if(!is_dir($filedir))
        {
                mkdir($filedir, 0777, true);
                chmod($filedir,0777);
        }
    }
    public static function houzui($name)
    {
        $nameArr = explode(".",$name);
        $houzui = end($nameArr);
        return strtolower($houzui);
    }
    ///判断图片的类型
    public static function pdpictype($adrfilename)
    {
        $mime = 'jpg';
//        $image = exif_imagetype($adrfilename);
//        if($image)
//        {
//                $mime = image_type_to_mime_type($image);
//        }
        return $mime;
    }
    /**
     *
     * @param type $name
     * @param type $type
     * @param type $size
     * @param string $adr : xx/xxx/ 格式
     * @return type
     */
    public static function upfile($name, $type = "jpg,gif,png,jpeg", $size = 0, $adr = "")
    {////字段，类型判断=img(全部类型)，上传大小，上传路径文件夹
        if(isset($_FILES[$name]))
        {
            $files = $_FILES[$name];
            return self::_upfile($files,$type,$size,$adr);
        }else
        {
            return "";
        }
    }
    public static function mulfile($name, $type = "jpg,gif,png,jpeg", $size = 0, $adr = "")
    {
        $files = array();
        if(isset($_FILES[$name]))
        {
            $mulfiles = $_FILES[$name];
            foreach($mulfiles['name'] as $key=>$value)
            {
                $files[] = array('name'=>$mulfiles['name'][$key],'type'=>$mulfiles['type'][$key],'tmp_name'=>$mulfiles['tmp_name'][$key]
                        ,'error'=>$mulfiles['error'][$key],'size'=>$mulfiles['size'][$key]);
            }
        }

        return $files;
    }
    public function up($name,$fold="xls")
    {
        if(isset($_FILES[$name]))
        {
            $files = $_FILES[$name];
            set_time_limit(60*20);
            $_tmpname = self::intserials('day');
            $_name = $files["name"];
            $_size = $files["size"];
            $_houzui = self::houzui($_name);
            $adr = "{$fold}/".date("Ym");
            self::madedir(PICUP."/".$adr);
            $filename = $adr."/".$_tmpname.".".$_houzui;
            $url = PICUP."/".$filename;
            move_uploaded_file($files["tmp_name"], $url);
            $arr = array("pic" => $filename, "picname" => $_name, "size" => $_size);
            return $arr;
        }else
        {
            return array();
        }
    }
    public static function intserials($type='day')
    {
        return date('YmdHis').'_'.rand(0,10000);
    }
    //直接上传到正式目录下
    public static function _upfile($files, $type = "jpg,gif,png,jpeg", $size = 0, $adr = "")
    {
        set_time_limit(60*20);
        $_tmpname = self::intserials('day');
        $_name = $files["name"];
        $_size = $files["size"];
        $tmp_name = $files['tmp_name'];
        $pdpictype = self::pdpictype($tmp_name);
        if ($_size > 0 && $pdpictype !="")
        {
                $_houzui = self::houzui($_name);
                $typeArr = explode(",",$type);
                $typeArr = array_flip($typeArr);
                if(!isset($typeArr[$_houzui]))
                {
                        self::pageback("", "上传类型不对，请上传 [ " . $type . " ] 类型", "");
                }
                $pic = $_tmpname . "." . $_houzui;
                if(empty($adr))
                {
                    $adr = "up/".date("Ym")."/";
                }else
                {
                    $adr = $adr."/".date("Ym")."/";
                }
                $todir = PICUP."/".$adr;
                self::madedir($todir);
                $filename = $adr.$pic;
                $url = PICUP .'/'. $filename;
                if ($size > 0) {
                        if ($_size > $size*1024) {
                                self::pageback("", "上传文件 $_size 过大，请小于 [ " . $size . " ] KB", "");
                        }
                }
                move_uploaded_file($files["tmp_name"], $url);
                //$arr = array("pic" => $filename, "picname" => $_name, "size" => $_size);
                return $filename;
        } else {
                return "";
        }
    }
    //上传到其他的路径，针对cos的
    //直接上传到正式目录下
    public static function cosupfile($filesname, $type = "jpg,gif,png,jpeg", $size = 0, $adr = "")
    {
        set_time_limit(60*20);
        $_tmpname = self::intserials('day');
        $uid = (int) CookieModel::getcookies('CI');

        $_tmpname = $_tmpname."_".$uid;
        $files = $_FILES[$filesname];
        $_name = $files["name"];
        $_size = $files["size"];
        $tmp_name = $files['tmp_name'];
        $pdpictype = self::pdpictype($tmp_name);
        if ($_size > 0 && $pdpictype !="")
        {
                $_houzui = self::houzui($_name);
                $typeArr = explode(",",$type);
                $typeArr = array_flip($typeArr);
                if(!isset($typeArr[$_houzui]))
                {
                        self::pageback("", "上传类型不对，请上传 [ " . $type . " ] 类型", "");
                }
                $pic = $_tmpname . "." . $_houzui;
                if(empty($adr))
                {
                    $adr = "cospic/".date("Ym")."/";
                }else
                {
                    $adr = $adr."/".date("Ym")."/";
                }
                $todir = PICUP."/".$adr;
                self::madedir($todir);
                $filename = $adr.$pic;
                $url = PICUP .'/'. $filename;
                if ($size > 0) {
                        if ($_size > $size*1024) {
                                self::pageback("", "上传文件 $_size 过大，请小于 [ " . $size . " ] KB", "");
                        }
                }
                $tf = move_uploaded_file($files["tmp_name"], $url);
                $arr = array();
                if($tf)
                {
                    DBData::printtxt($tf, "upload");
//                    $arr = array("pic" => $filename, "picname" => $_name, "size" => $_size);
                    $imgsize = getimagesize($url);
                    $width = $imgsize[0];
                    $whxy = array('w'=>200,'h'=>300);
                    $_tmp_dir = '/picup/';
                    DBData::printtxt(json_encode($imgsize,256), "upload");
                    $fileName2= cutpicModel::cutpic($whxy,$filename,$_tmp_dir,$_tmp_dir,"200");

                    if($width>800)
                    {
                        //图片宽度大于1600的时候，剪切为1500的宽
                        cutpicModel::setw(750);
                        cutpicModel::setisdel(0);///删除原来的文件
                        $xywh=array();
                        $fileName_s=cutpicModel::suoxiaopic($xywh,$filename,$_tmp_dir,$_tmp_dir,"750");
                    }else
                    {
                        $fileName_s = $filename;
                    }
                    $arr = array('pic'=>$filename,'pic_s'=>$fileName_s,'pic2'=>$fileName2);
                    ///是否加水印
                    $iswater = 1;
                    if($iswater == 1)
                    {
//					import('class.ShuiYinClass');
                            ShuiYinModel::imageWaterMark($filename, 9,'set',$_tmp_dir);
                            ShuiYinModel::imageWaterMark($fileName_s, 9,'set',$_tmp_dir);
                    }
                    if(!empty($fileName2))
                    {
                        $picstr = $filename."|".$fileName_s."|".$fileName2;
                        DBData::printtxt($picstr, "webpicup");
                        echo $picstr;
                    }else
                    {
                        echo "";
                    }
                }
                echo "";
        } else {
                echo "";
        }
    }

    public static function delpic($pic)
    {
        if(!empty($pic))
        {
            @unlink(PICUP .'/'. $pic);
        }

    }

    public static function base64($image,$houzui='jpg',$filename="",$path = "")
    {
        $backarr = array();
        if(empty($image))
        {
            $backarr = array('status'=>'fail','msg'=>'没有图片','pic'=>'');
            return $backarr;
        }
        if (strstr($image,",")){
            $imageArr = explode(',',$image);
            $image = $imageArr[1];
        }

        if(empty($path)){
            $path = "up/".date("Ym");
        }else
        {
            $path = $path."/".date("Ym");
        }
        if(empty($filename)){
            $filename = self::intserials('day');
        }
        $picup_root = PICUP."/";
        //创建目录并设置权限
        $savedir = $picup_root.$path;
        self::madedir($savedir);

        $url = $path.'/'.$filename.".".$houzui;

        $r = file_put_contents($picup_root.$url, base64_decode($image));
        if(!$r)
        {
            $backarr = array('status'=>'fail','msg'=>'上传失败','size'=>$r,'pic'=>'','abs'=>$picup_root.$url);
        }else
        {
            $backarr = array('status'=>'ok','msg'=>'上传成功','size'=>$r,'pic'=>$url,'abs'=>$picup_root.$url);
        }
        return $backarr;
    }
    //base64直接返回图片路径
    public static function base64topic($image,$houzui='jpg',$filename="",$path = "")
    {
        $pic = '';
        if(empty($image))
        {
            return $pic;
        }
        if (strstr($image,",")){
            $imageArr = explode(',',$image);
            $image = $imageArr[1];
        }

        if(empty($path)){
            $path = "up/".date("Ym");
        }else
        {
            $path = $path."/".date("Ym");
        }
        if(empty($filename)){
            $filename = self::intserials('day');
        }
        $picup_root = PICUP."/";
        //创建目录并设置权限
        $savedir = $picup_root.$path;
        self::madedir($savedir);

        $url = $path.'/'.$filename.".".$houzui;

        $r = file_put_contents($picup_root.$url, base64_decode($image));
        if($r)
        {
            $pic = $url;
        }
        return $pic;
    }

    public static function url_downpic($url,$filename="",$dir='')
    {
        if(empty($url))
        {
            return array('status'=>'fail','msg'=>'地址为空','pic'=>'');
        }
        if(empty($dir))
        {
            $dir = 'up/'.date('Ym');
        }
        self::madedir(PICUP.'/'.$dir);
        if(empty($filename))
        {
            $filename = ConstModel::getserials().".jpg";
        }
        $filename = $dir.'/'.$filename;
        $content = file_get_contents($url);
        $r = file_put_contents(PICUP.'/'.$filename, $content);
        if($r)
        {
            return array('status'=>'ok','msg'=>'ok','pic'=>$filename);
        }else
        {
            return array('status'=>'fail','msg'=>'图片生成失败');
        }
    }


}
